package com.base.controller;

import com.base.comm.CommContent;
import com.base.po.voip.Porder;
import com.base.service.LogsService;
import com.base.service.PorderService;
import com.base.utils.IP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class PorderUpdateController {

	@Autowired
	private LogsService logsService; // 日志Service
	@Autowired
	private PorderService porderService;

	@RequestMapping(value = "/porder_do_update", method = RequestMethod.POST)
	public @ResponseBody int doUpdate(HttpServletRequest request, Porder porder) throws Exception {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}
		int flag; // 成功状态
		if (porderService.select() == null) {
			flag = porderService.insertSelective(porder); // 新增
		} else {
			flag = porderService.update(porder.getAgentid()); // 修改
		}
		logsService.insert(CommContent.TYPE_UPDATE, CommContent.LOGS_UPDATE + "改动返佣信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return flag;
	}
}
